/*
 * CommonModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.ITag;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.data.request.FeedBackRequest;
import com.yidiandao.business.data.request.SendErrorInfoRequest;
import com.yidiandao.business.data.response.HelpResponse;
import com.yidiandao.business.data.response.LocationResponse;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.response.SplashResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.HelpCenterListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 其他比较散乱的model
 */
public class CommonModel implements ICommonModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getHelpCenterInfo(
            String tag, final onAccessNetListener<List<DisplayListItem>> listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setUrl(Urls.ARTICLE_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, HelpResponse.class, new NetListener<HelpResponse>() {
            @Override
            public void onSuccess(HelpResponse helpResponse) {
                List<HelpResponse.DataBean.ArticleListBean> articleList =
                        helpResponse.getData().getArticleList();

                ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                for (HelpResponse.DataBean.ArticleListBean bean : articleList) {
                    displayListItems.add(new HelpCenterListItem(bean));
                }
                if (listener != null) {
                    listener.onSuccess(displayListItems);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void sendFeedBackInfo(
            String tag, String info, String connection, final onAccessNetListener listener) {
        FeedBackRequest request = new FeedBackRequest();
        request.setTag(tag);
        request.setUrl(Urls.FEED_BACK);
        request.setSuggest(info);
        request.setConnection(connection);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess("");
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void sendErrorInfo(String tag, String msg) {
        SendErrorInfoRequest request = new SendErrorInfoRequest();
        request.setTag(tag);
        request.setUrl(Urls.ERROR_MSG);
        request.setMsg(msg);
        request.setType("Android");
        mNetBiz.sendPostRequest(request, null, null);
    }

    @Override
    public void getCityList(final String tag, final onGetDataListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setUrl(Urls.GET_CITY_LIST);
        mNetBiz.sendGetRequest(request, LocationResponse.class,
                new NetListener<LocationResponse>() {
                    @Override
                    public void onSuccess(LocationResponse response) {
                        LocationResponse.DataBean data = response.getData();
                        List<Tag> tags = new ArrayList<>();
                        List<String> citys = new ArrayList<>();
                        if (null != data) {
                            List<String> cityList = data.getCityList();
                            if (null != cityList) {
                                citys.addAll(cityList);
                                for (String city : cityList) {
                                    tags.add(new Tag(city));
                                }
                            }
                        }

                        if (listener != null) {
                            listener.onSuccess(tags, citys);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void getSplashAD(String tag, final onAccessNetListener<SplashResponse.DataBean.PictureInfoBean> listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setUrl(Urls.APP_GUIDE);
        mNetBiz.sendGetRequest(request, SplashResponse.class, new NetListener<SplashResponse>() {
            @Override
            public void onSuccess(SplashResponse splashResponse) {
                if (listener != null) {
                    listener.onSuccess(splashResponse.getData().getPictureInfo());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }
}
